<?php
namespace app\admin\controller;
class Shareurl extends Common
{

	 /* //共享网址
    public function index()
    {   
        if(request() -> isPost()){
            $key = input('post.key');
            $page = input('page')?input('page'):1;
            $pageSize =input('limit')?input('limit'):10;
            $list = DB::table('pre_common_block_shareurl')
                 ->alias('a')
                 ->join('pre_common_block_cate b','a.cid = b.id')
                 ->field('b.name,a.*')
                 -> where('a.id|a.title','like','%'.$key.'%')
                 ->order('id desc')
                 -> paginate(array('list_rows'=>$pageSize,'page'=>$page))
                 ->toArray();   
            return json(['code'=>0,'msg'=>'获取成功!','data'=>$list['data'],'count'=>$list['total'],'rel'=>1]);
        }
        return $this->fetch();
    }*/

    //共享网址
    public function index()
    {   
        if(request() -> isPost()){
            $key = input('post.key');
            $page = input('page')?input('page'):1;
            $pageSize =input('limit')?input('limit'):10;
            $list = db('common_block_shareurl')
                 ->alias('a')
                 -> where('id|title','like','%'.$key.'%')
                 ->order('id desc')
                 -> paginate(array('list_rows'=>$pageSize,'page'=>$page))
                 ->toArray();  
            foreach ($list['data'] as $key => $value) {
                $name = $this->urlid($value['cid']);
                $list['data'][$key]['name'] =  $name;
            }
            return json(['code'=>0,'msg'=>'获取成功!','data'=>$list['data'],'count'=>$list['total'],'rel'=>1]);
        }
        return $this->fetch();
    }

    public function urlid($id){
        $cate = db('common_block_cate')->field('id,name')->select();  
        foreach ($cate as $k => $v){
            if($v['id'] == $id){
              return $v['name'];
            }
        }
    }

    //添加共享网址分类
    public function add()
    {   
        if(request() ->isPost()){
          $data['username'] = trim(input('post.username'));
          $data['password'] = trim(input('post.password'));
          $data['user'] = trim(input('post.user'));
          $data['pass'] = trim(input('post.pass'));
          $data['title'] = trim(input('post.title'));
          $data['url'] = trim(input('post.url'));
          $data['cid'] = input('post.cid');
          $data['addtime'] = date('Y-m-d H:i:s');
          $cate = db('common_block_shareurl')->insertGetId($data);
          if($cate){
                return json(['code'=>1,'msg'=>'添加成功','url' => url('shareurl/index')]);
          }else{
                return json(['code'=>0,'msg'=>'添加失败']);
          }
        }else{    
           $list = db('common_block_cate')->field('id,name')->select();
           $this->assign('list',$list);
           return $this->fetch();
        }
    }

    //编辑共享网址分类
    public function edit($id='')
    {
        if(request() -> isPost()){
            $id = input('post.id');
            $data = input('post.');
            $update =db('common_block_shareurl')->where('id', $id)->update($data);
            if($update){
                return json(['code'=>1,'msg'=>'编辑成功','url' => url('shareurl/index')]);
            }else{
                return json(['code'=>0,'msg'=>'编辑失败']);
            }
        }else{
            $list = db('common_block_shareurl')->where('id',$id)->find();
            $cate = db('common_block_cate')->field('id,name')->select();
            $this->assign('list',$list);
            $this->assign('cate',$cate);
            return $this->fetch();
        }
    }

    //删除单条记录(删除数据)
    public function del()
    {   
        $data['id'] = input('param.id');  
        if($data){
            $del = db('common_block_shareurl')->where($data)->delete();
            if($del){
                return json(['code'=>1,'msg'=>'删除成功']);
            }else{
                return json(['code'=>0,'msg'=>'删除失败']);
            }
        }
        
    }

    //删除单条记录(删除数据)
    public function delall()
    {   
        if(request() -> isPost()){
            $ids = $_POST['ids'];  
            $all = db('common_block_shareurl')->delete($ids);
            if($all){
                return json(['code'=>1,'msg'=>'删除成功']);
            }else{
                return json(['code'=>0,'msg'=>'删除失败']);
            }
        }
    }
}